package com.yic.module.system.controller.admin.code;

import com.yic.module.system.convert.code.CodeRuleConvert;
import com.yic.module.system.dal.dataobject.code.CodeRuleDO;
import com.yic.module.system.service.code.CodeRuleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.system.controller.admin.code.vo.*;
import com.yic.module.system.dal.dataobject.code.CodeDO;
import com.yic.module.system.convert.code.CodeConvert;
import com.yic.module.system.service.code.CodeService;

@Tag(name = "管理后台 - 编号规则")
@RestController
@RequestMapping("/system/code")
@Validated
public class CodeController {

    @Resource
    private CodeService codeService;
    @Resource
    private CodeRuleService codeRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建编号规则")
    @PreAuthorize("@ss.hasPermission('system:code:create')")
    public CommonResult<Long> createCode(@Valid @RequestBody CodeCreateReqVO createReqVO) {
        return success(codeService.createCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编号规则")
    @PreAuthorize("@ss.hasPermission('system:code:update')")
    public CommonResult<Boolean> updateCode(@Valid @RequestBody CodeUpdateReqVO updateReqVO) {
        codeService.updateCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编号规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:code:delete')")
    public CommonResult<Boolean> deleteCode(@RequestParam("id") Long id) {
        codeService.deleteCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编号规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:code:query')")
    public CommonResult<CodeRespVO> getCode(@RequestParam("id") Long id) {
        CodeDO code = codeService.getCode(id);
        List<CodeRuleDO> codeRules = codeRuleService.getCodeRuleByCodeId(id);
        List<CodeRespVO.Rule> rules = CodeRuleConvert.INSTANCE.convertList05(codeRules);
        return success(CodeConvert.INSTANCE.convert(code, rules));
    }

    @GetMapping("/list")
    @Operation(summary = "获得编号规则列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:code:query')")
    public CommonResult<List<CodeRespVO>> getCodeList(@RequestParam("ids") Collection<Long> ids) {
        List<CodeDO> list = codeService.getCodeList(ids);
        return success(CodeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编号规则分页")
    @PreAuthorize("@ss.hasPermission('system:code:query')")
    public CommonResult<PageResult<CodeRespVO>> getCodePage(@Valid CodePageReqVO pageVO) {
        PageResult<CodeDO> pageResult = codeService.getCodePage(pageVO);
        return success(CodeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编号规则 Excel")
    @PreAuthorize("@ss.hasPermission('system:code:export')")
    @OperateLog(type = EXPORT)
    public void exportCodeExcel(@Valid CodeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CodeDO> list = codeService.getCodeList(exportReqVO);
        // 导出 Excel
        List<CodeExcelVO> datas = CodeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "编号规则.xls", "数据", CodeExcelVO.class, datas);
    }

    @GetMapping("/gen")
    @Operation(summary = "生成编号")
    @Parameter(name = "code", description = "规则代码", required = true, example = "1024")
    public CommonResult<String> genCode(@RequestParam("code") String code){
        return success(codeService.genCode(code));
    }

}
